#pragma once

#include <vector>
#include <glm/glm.hpp>
#include <memory>
#include "engine/core/camera2d.h"
#include "engine/platform/shader.h"

namespace kanon
{
    struct PolygonVertex
    {
        glm::vec2 pos;
        glm::vec4 color;
    };

    struct PolygonBufferBatch
    {
    private:
        friend class PolygonRenderer;
        uint32_t vao = -1;
        uint32_t vbo = -1;

        uint32_t max_vertices = 0;
        uint32_t num_vertices = 0;
        PolygonVertex* vertex_base;
        
    public:
        PolygonBufferBatch(uint32_t max_segments);
        ~PolygonBufferBatch();
        void Begin();
        void End();
        bool Submit(const glm::vec2& pt1, const glm::vec2& pt2, const glm::vec4& color);

    };

    class PolygonRenderer
    {
    private:
        std::shared_ptr<Shader> shader = nullptr;
        std::shared_ptr<PolygonBufferBatch> batch = nullptr;
        std::shared_ptr<Camera2d> camera = nullptr;

    public:
        PolygonRenderer();
        ~PolygonRenderer() {}

        void Render(const glm::vec2& pt1, const glm::vec2& pt2, const glm::vec4& color);
        void Render(const std::vector<glm::vec2>& points, const glm::vec4& color, bool loop = true);
        void SetCamera(const std::shared_ptr<Camera2d>& cam);
        void SetLineWidth(float width);
        void Begin();
        void End();
        void Flush();
    };
}
